﻿@{
    ViewBag.Title = "WorkAuditSetting";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<link href="~/Content/scripts/plugins/jqgrid/jqgrid.css" rel="stylesheet" />
<script src="~/Content/scripts/plugins/jqgrid/jqgrid.min.js"></script>
<script>
    var keyValue = request('keyValue');
    $(function () {
        initControl();
    })

    //初始化控件
    function initControl() {
        GetGrid();
        //获取表单
        if (!!keyValue) {
            $.SetForm({
                url: "../../SystemManage/Setting/GetWorkAuditEntity",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#form1").SetWebControls(data);
                    $("#gridTable")[0].addJSONData(eval(data.AuditMemo));
                }
            });
        }
    }

    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            datatype: "json",
            height: $(window).height() - 200,
            autowidth: true,
            unwritten: false,
            colModel: [
                { label: '主键', name: 'Id', hidden: true },
                { label: '审批名称', name: 'Name', index: 'Name', width: 200, align: 'left' },
                { label: '级数', name: 'Level', index: 'Level', width: 200, align: 'left' },
                { label: '执行类别', name: 'RoleType', index: 'RoleType', width: 200, align: 'left', hidden: true },
                { label: '角色Id', name: 'RoleId', index: 'RoleId', width: 200, align: 'left', hidden: true },
                { label: '执行类别', name: 'RoleTypeName', index: 'RoleTypeName', width: 200, align: 'left' }, 
                { label: '角色', name: 'RoleName', index: 'RoleName', width: 200, align: 'left' },
                { label: '是否有处理内容', name: 'HasHandling', index: 'HasHandling', width: 200, align: 'left', hidden: true },
                { label: '内容Json', name: 'HandleConditionJson', index: 'HandleConditionJson', width: 200, align: 'left', hidden: true },
                { label: '是否有流转条件限制（0-否 1-是）', name: 'HasContion', index: 'HasContion', width: 200, align: 'left', hidden: true },
                { label: '流转条件Json', name: 'ConditionJson', index: 'ConditionJson', width: 200, align: 'left', hidden: true },
                { label: '节点数据处理Json', name: 'SetInfo', index: 'SetInfo', width: 200, align: 'left', hidden: true }, 
                { label: '驳回步骤', name: 'RejectLevel', index: 'RejectLevel', width: 200, align: 'left', hidden: true },
            ],
            rowNum: "10000",
            rownumbers: true,
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
    }

    //添加工单审核角色
    function btn_add() {
        var Level = $("#gridTable").jqGrid('getDataIDs').length + 1;
        if (Level <= $("#AuditLevel").val()) {
            dialogOpen({
                id: "WorkAuditSettingForm",
                title: '添加工单审核角色',
                url: '/SystemManage/Setting/WorkAuditSettingForm?Level=' + Level,
                width: "700px",
                height: "550px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
        else { dialogMsg('当前审核级数无法在新增审核角色！', 0); }
    };
    //修改
    function btn_edit() {
        var RowId = $("#gridTable").getGridParam('selrow');
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        var Level = $("#gridTable").jqGridRowValue("Level");
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "WorkAuditSettingForm",
                title: '修改工单审核角色',
                url: '/SystemManage/Setting/WorkAuditSettingForm?keyValue=' + keyValue + "&Level=" + Level + "&RowId=" + RowId,
                width: "700px",
                height: "550px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
    }
    //删除
    function btn_delete() {
        var keyValue = $("#gridTable").getGridParam('selrow');
        var Level = $("#gridTable").jqGrid('getRowData', keyValue).Level;
        if (keyValue) {
            if (Level != $("#AuditLevel").val()) { dialogMsg('只能从最后一条开始移除！', 0); return false; }
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable").delRowData(keyValue);
                    dialogMsg("移除成功。", 1);
                }
            });
        } else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }

    //保存表单
    function AcceptClick(obj) {
        if (!$('.form').Validform()) {
            return false;
        }
        var Level = $("#gridTable").jqGrid('getDataIDs').length;
        if (Level != $("#AuditLevel").val()) { dialogMsg('当前审核级数不匹配！', 0); return false; }
        var postData = $(".form").GetWebControls(keyValue);
        postData["AuditMemo"] = JSON.stringify($("#gridTable").jqGrid("getRowData"));
        $.SaveForm({
            url: "/SystemManage/Setting/SaveWorkAuditSetting?keyValue=" + keyValue,
            param: postData,
            loading: "正在保存数据...",
            success: function () {
                if (!!!$.currentIframe().$("#WorkAudit"))
                    $.currentIframe().GetWorkAuditHtml();
            }
        }); 

    };
</script>
<table class="form">
    <tr>
        <th class="formTitle">审核级数</th>
        <td class="formValue">
            <input id="AuditLevel" type="number" class="form-control" isvalid="yes" checkexpession="NotNull">
        </td>
    </tr>
    <tr>
        <th class="formTitle">当前版本</th>
        <td class="formValue">
            <input id="Version" type="text" class="form-control" isvalid="yes" checkexpession="NotNull">
        </td>
    </tr>
    <tr>
        <th class="formTitle">附加说明</th>
        <td class="formValue">
            <textarea id="Description" placeholder="暂无附加说明" class="form-control " style="height: 50px;" checkexpession="IsScript"></textarea>
         </td>
    </tr>
</table>
<div class="toolbar" style="height:41px;padding:5px;">
    <div class="btn-group" style="float:right;">
        <a id="lr-add" class="btn btn-success btn-xs" onclick="btn_add()"><i class="fa fa-plus"></i>&nbsp;添加</a>
        <a id="lr-edit" class="btn btn-info btn-xs" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;编辑</a>
        <a id="lr-delete" class="btn btn-danger btn-xs" onclick="btn_delete()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
    </div>
</div>
<table id="gridTable"></table>
